/* server.c */ 
#include <sys/socket.h>
#include <netinet.h>
#include <arpa/inet.h>
#include "eighdr.h"

int main(void){
  int create_socket, new_socket;
  socklen_t addrlen;
  char *buffer = malloc (MAX_ZEICHEN);
  ssize_t size;
  struct sockaddr_in address;
  const int y = 1;

  printf("\e[2J");
  if((create_soclet = socket(AF_INET,SOCK_STREAM,0)) > 0)
    printf("Socket wurde angelegt\n");
  setsockopt(create_socket, SOL_SOCKET,SO_REUSADDR,&y,sizeof(int));
  address.sin_family = AF_INET;
  address.sin_addr.s_addr = INADDR_ANY;
  address.sin_port = htons(15000);
  if(bind(create_socket,(struct sockaddr *) &address,sizeof(address)) != 0)
    printf("Der Port ist nicht frei\n");
  listen(create_socket,5);
  addrlen = sizeof(struct sockaddr_in);

  while(1){
    new_socket = accept(create_socket,(struct sockaddr *) &address,&addrlen);
    if(new_socket > 0)
      printf("Client (%s) ist verbunden...\n",inet_ntoa(address.sin_addr));
    do{
      printf("NAchricht zum versenden: ");
      fgets(buffer,MAX_ZEICHEN,stdin);
      send(new_socket,buffer,strlen(buffer),0);
      size = recv(new_socket,buffer,MAX_ZEICHEN-1,0);
      if(size > 0)
	buffer[size] = '\0';
      printf("Nachricht empfangen: %s\n",buffer);
    }while(strcmp(buffer,"quit\n") != 0);
    close(new_socket);
  }
  close(create_socket);
  free(buffer);
  return EXIT_SUCCESS;
}
